/*  search.cpp
 * Copyright (C) 2010, Francisco Claude & Patrick Nicholson, all rights reserved.
 *
 * Francisco Claude & Patrick Nicholson <(fclaude|p3nichol)@cs.uwaterloo.ca>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<cassert>
#include<cmath>

using namespace std;

#include "search.h"

size_t searchChainCrossDes(vector<Point> & v, const Point * chain, const Point & p1, const Point & p2, const size_t len) {
  size_t count = 0;
  size_t ini = 0, fin = len-1;
  while(ini < fin) {
    size_t med = (ini+fin)/2;
    
    double x = chain[med].getX();
    double y = chain[med].getY();

    if(x>p2.getX() || y<p1.getY()) {
      if(med==0) fin = med;
      else fin = med-1;
    } else if(x<p1.getX() || y>p2.getY()) {
      ini = med+1;
    } else {
      fin = med;
      break;
    }
  }

  if(inside(chain[fin],p1,p2)) {
    if(fin>0 && fin<len-1) {
      v.push_back(chain[fin]);
      count++;
    }
    size_t i = fin+1;
    while(i < len-1) {
      if(inside(chain[i],p1,p2)) {
        v.push_back(chain[i]);
        count++; i++;
      } else break;
    }
    i = fin-1;
    while(i+1 > 1) {
      if(inside(chain[i],p1,p2)) {
        v.push_back(chain[i]);
        count++; i--;
      } else break;
    }
    return HIT;
  }

  if(fin == len-1) {
    if(cross(&chain[fin-1],&chain[fin],&p1,&p2)) return CROSS;
    if(p2.getX() <= chain[fin].getX() && p1.getY() >= chain[fin].getY()) { // 4
      if(chain[fin-1].getX()>=p2.getX() && chain[fin-1].getY()>=p1.getY()) return RIGHT;
      if(!left(&chain[fin-1],&chain[fin],&p2)) return RIGHT; 
    }
    return LEFT;
  }
    
  if(fin==0) {
    if(cross(&chain[fin],&chain[fin+1],&p1,&p2)) return CROSS;
    if(chain[fin].getX() <= p1.getX() && chain[fin].getY()>=p2.getY()) { 
      if(chain[fin+1].getX() <= p2.getX() && chain[fin+1].getY() >= p2.getY()) return RIGHT;
      if(left(&chain[fin],&p2,&chain[fin+1])) return RIGHT;
    }
    return LEFT;
  }

  if(cross(&chain[fin],&chain[fin+1],&p1,&p2)) return CROSS;
  if(cross(&chain[fin-1],&chain[fin],&p1,&p2)) return CROSS;

  if(p1.getX() <= chain[fin].getX()) {
    if(p1.getY()>=chain[fin].getY()) {
      if(left(&chain[fin-1],&chain[fin],&p1)) return LEFT;
      return RIGHT;
    }
    return RIGHT;
  }

  if(chain[fin].getY()>=p2.getY()) { 
    if(chain[fin+1].getX() <= p2.getX() && chain[fin+1].getY() >= p2.getY()) return RIGHT;
    if(left(&chain[fin],&p2,&chain[fin+1])) return RIGHT;
    return LEFT;
  }
 
  return LEFT; 
}

size_t searchChainHitsDes(vector<Point> & v, const Point * chain, const Point & p1, const Point & p2, const size_t len) {
  size_t count = 0;
  size_t ini = 1, fin = len-2;
  while(ini < fin) {
    size_t med = (ini+fin)/2;
    
    double x = chain[med].getX();
    double y = chain[med].getY();

    if(x>p2.getX() || y<p1.getY()) {
      if(med==0) fin = med;
      else fin = med-1;
    } else if(x<p1.getX() || y>p2.getY()) {
      ini = med+1;
    } else {
      fin = med;
      break;
    }
    bool c = compatibleDes(chain[med],p1) || compatibleDes(p1,chain[med]);
    c |= compatibleDes(chain[med],p2) || compatibleDes(p2,chain[med]);
    if(!c) return 0;
  }

  if(inside(chain[fin],p1,p2)) {
    v.push_back(chain[fin]);
    count++;
    size_t i = fin+1;
    while(i < len-1) {
      if(inside(chain[i],p1,p2)) {
        v.push_back(chain[i]);
        count++; i++;
      } else break;
    }
    i = fin-1;
    while(i > 0) {
      if(inside(chain[i],p1,p2)) {
        v.push_back(chain[i]);
        count++; i--;
      } else break;
    }
  }
  return count;
}

